package com.sptpc.company.util;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeUtil {
	// 通用的格式
	private static final String COMMON_FORMATE = "yyyy-MM-dd HH:mm:ss";

	// 字符串转换为date
	public static Date strToDate(String str, String formateStr) {
		DateTimeFormatter dtf = DateTimeFormat.forPattern(formateStr);
		DateTime dateTime = dtf.parseDateTime(str);
		return dateTime.toDate();

	}

	// date转换为字符串
	public static String dateToStr(Date date, String formateStr) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(formateStr);
	}

	// 字符串转date
	public static Date strToDate(String str) {
		DateTimeFormatter dft = DateTimeFormat.forPattern(COMMON_FORMATE);
		DateTime dateTime = dft.parseDateTime(str);
		return dateTime.toDate();
	}

	// date转字符串
	public static String dateToStr(Date date) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(COMMON_FORMATE);
	}
}
